home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / recio215.zip / RCPUTT.C < prev    next >
C/C++ Source or Header  |  1996-10-26  |  4KB  |  103 lines

  1. /*****************************************************************************
  2.    MODULE: rcputt.c
  3.   PURPOSE: recio column delimited time output functions
  4. COPYRIGHT: (C) 1994-1996, William Pierpoint
  5.  COMPILER: Borland C Version 3.1
  6.        OS: MSDOS Version 6.2
  7.   VERSION: 2.15
  8.   RELEASE: October 26, 1996
  9. *****************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <time.h>
  15.  
  16. #include "recio.h"
  17.  
  18. extern int _risready(REC *rp, int mode);
  19. extern int _rputc(REC *rp, int ch);
  20.  
  21. #define rflags(rp)       ((rp)->r_flags)
  22. #define rfp(rp)          ((rp)->r_fp)
  23. #define rcol(rp)         ((rp)->r_colno)
  24. #define rfldch(rp)       ((rp)->r_fldch)
  25. #define rtmfmt(rp)       ((rp)->r_tmfmt)
  26.  
  27. /****************************************************************************/
  28. void                         /* returns nothing                             */
  29.     rcputtm(                 /* put time to record stream                   */
  30.         REC *rp,             /* record pointer                              */
  31.         size_t begcol,       /* field inclusive beginning column            */
  32.         size_t endcol,       /* field inclusive ending column               */
  33.         struct tm t)         /* broken-down time                            */
  34. /****************************************************************************/
  35. {
  36.     size_t maxsize;          /* max size of str buffer */
  37.     size_t len;              /* actual length of str */
  38.     char *str=NULL;          /* storage for output string */
  39.     int errnum;              /* error number */
  40.  
  41.     if (_risready(rp, R_WRITE)) {
  42.         if (endcol >= begcol && begcol >= rcolno(rp)) {
  43.             rfldno(rp)++;
  44.             rflags(rp) &= ~_R_TXT;
  45.  
  46.             /* if colno < begcol, pad with spaces */
  47.             while (rcolno(rp) < begcol) {
  48.                 if (_rputc(rp, ' ')) goto done;
  49.             }
  50.  
  51.             /* put time into formatted string */
  52.             maxsize = strlen(rtmfmt(rp));  /* arbitrary initial max size   */
  53.             do {                           /* loop until max size adequate */
  54.                 maxsize += 16;             /* arbitrary max size increment */
  55.                 do {                       /* loop until memory allocated  */
  56.                     str = (char *) realloc(str, maxsize+1);
  57.                     if (!str) {
  58.                         errnum = rseterr(rp, R_ENOMEM);
  59.                         if (errnum) goto done;
  60.                     }
  61.                 } while (!str);
  62.                 len = strftime(str, maxsize, rtmfmt(rp), &t);
  63.             } while (!len);
  64.     
  65.             /* if str fits within the space, output it */
  66.             if (len <= (endcol-begcol+1)) {
  67.                 if (fputs(str, rfp(rp)) != EOF) { 
  68.                     rcol(rp) += len;
  69.                     /* pad remainder of space */
  70.                     while (rcolno(rp) <= endcol) {
  71.                         if (_rputc(rp, ' ')) goto done;
  72.                     }
  73.                 } else {
  74.                     rseterr(rp, R_ENOPUT);
  75.                 }
  76.             /* else converted string too long for space */
  77.             } else {
  78.                 rsetwarn(rp, R_WWIDTH);
  79.                 while (rcolno(rp) <= endcol) {
  80.                     if (_rputc(rp, '*')) goto done;
  81.                 }
  82.             }
  83.         } else {
  84.             rseterr(rp, R_EINVAL); \
  85.         }
  86.     }
  87. done:
  88.     free(str);
  89.     return;
  90. }
  91.  
  92. /****************************************************************************/
  93. void                         /* returns nothing                             */
  94.     rcputt(                  /* put time to record stream                   */
  95.         REC *rp,             /* record pointer                              */
  96.         size_t begcol,       /* field inclusive beginning column            */
  97.         size_t endcol,       /* field inclusive ending column               */
  98.         time_t time)         /* time                                        */
  99. /****************************************************************************/
  100. {
  101.     rcputtm(rp, begcol, endcol, timetotm(time));
  102. }
  103.